package com.algebra.util.sensitive;

import java.util.function.Function;

/**
 * @author al
 * @date 2022/5/25 14:03
 * @description
 */
public class DataFieldSensitiveUtil {

    public static String chineseName(String var0) {
        return action(var0, (var0x) -> {
            return SensitiveStrUtil.positiveString(
                    SensitiveStrUtil.subStringToIdx(var0x, 1),
                    SensitiveStrUtil.getCharLength(var0x),
                    "*");
        });
    }

    public static String idCard(String var0) {
        return action(var0, (var0x) -> {
            return SensitiveStrUtil.reverseString(SensitiveStrUtil.subStringFromIdx(var0x, 4), SensitiveStrUtil.getCharLength(var0x), "*");
        });
    }

    public static String phone(String var0) {
        return action(var0, (var0x) -> {
            return SensitiveStrUtil.reverseString(SensitiveStrUtil.subStringFromIdx(var0x, 4), SensitiveStrUtil.getCharLength(var0x), "*");
        });
    }

    public static String mobile(String var0) {
        return action(var0, (var0x) -> {
            return SensitiveStrUtil.subStringToIdx(var0x, 3)
                    .concat(SensitiveStrUtil.mobile(SensitiveStrUtil.reverseString(
                            SensitiveStrUtil.subStringFromIdx(var0x, 2), SensitiveStrUtil.getCharLength(var0x), "*"),
                            "***"));
        });
    }

    public static String address(String var0) {
        return action(var0, (var0x) -> {
            int charLength = SensitiveStrUtil.getCharLength(var0x);
            return charLength <= 8 ? var0x :
                    SensitiveStrUtil.positiveString(
                            SensitiveStrUtil.subStringFromIdx(var0x, charLength - 8), charLength, "*");
        });
    }

    public static String email(String var0) {
        return action(var0, (var0x) -> {
            int emailMarkIdx = SensitiveStrUtil.getCharIndex(var0x, "@");
            byte var2 = 1;
            if (emailMarkIdx > 5) {
                var2 = 3;
            }
            return 1 == emailMarkIdx ? var0x : SensitiveStrUtil.positiveString(SensitiveStrUtil.subStringToIdx(var0x, var2), emailMarkIdx, "*")
                    .concat(SensitiveStrUtil.email(var0x, emailMarkIdx, SensitiveStrUtil.getCharLength(var0x)));
        });
    }

    public static String carNumber(String var0) {
        return action(var0, (var0x) -> {
            return SensitiveStrUtil.subStringToIdx(var0x, 2).concat(SensitiveStrUtil.mobile(SensitiveStrUtil.reverseString(
                    SensitiveStrUtil.subStringFromIdx(var0x, 1),
                    SensitiveStrUtil.getCharLength(var0x), "*"), "**"));
        });
    }

    private static String action(String value, Function<String, String> function) {
        return SensitiveStrUtil.isWhitespace(value) ? null : function.apply(value);
    }

}
